﻿using Kaskelot.SP.Core;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Stockholm.SOA.Kaskelot.WCF.FaultContracts;
using Stockholm.SOA.Kaskelot.WCF.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Kaskelot.SP.Services
{
    public class BlankettService : BaseService<IKaskelotService>
    {
        public void DeleteBlankettPost(Guid id)
        {
            base.CheckWriteAllowed();

            try
            {
                this.channel.DeleteBlankettPost(ApplicationContext.Current.Val.Id, id);
            }
            catch (FaultException<ErrorInformation> ex)
            {
                Logger.LogError(this, LayerNames.Services, "DeleteBlankettPost", "Error deleting BlankettPost", ex);
                throw new ApplicationException(ex.Detail.ServiceError);
            }
        }

        public void UpdateBlankettPost(BlankettPost blankettPost)
        {
            base.CheckWriteAllowed();

            try
            {
                this.channel.UpdateBlankettPost(blankettPost);
            }
            catch (FaultException<ErrorInformation> ex)
            {
                Logger.LogError(this, LayerNames.Services, "UpdateBlankettPost", "Error updating BlankettPost", ex);
                throw new ApplicationException(ex.Detail.ServiceError);
            }
        }

        public BlankettPost GetBlankettPost(Guid id)
        {
            BlankettPost result = null;
            try
            {
                result = this.channel.GetBlankettPost(ApplicationContext.Current.Val.Id, id);
            }
            catch (FaultException<ErrorInformation> ex)
            {
                Logger.LogError(this, LayerNames.Services, "GetBlankettPost", "Error getting blankettpost", ex);
                throw new ApplicationException(ex.Detail.ServiceError);
            }
            return result;
        }

        public BlankettPost GetBlankettPostByPerson(Guid id)
        {
            BlankettPost result = null;
            try
            {
                result = this.channel.GetBlankettPostByPerson(ApplicationContext.Current.Val.Id, id);
            }
            catch (FaultException<ErrorInformation> ex)
            {
                Logger.LogError(this, LayerNames.Services, "GetBlankettPost", "Error getting blankettpost by person", ex);
                throw new ApplicationException(ex.Detail.ServiceError);
            }
            return result;
        }

        public FaltAlternativ[] GetFaltAlternativForFalt(Guid faltId)
        {
            FaltAlternativ[] fa = null;
            try
            {
                fa = this.channel.GetFaltAlternativForFalt(ApplicationContext.Current.Val.Id, faltId);
            }
            catch (FaultException<ErrorInformation> ex)
            {
                Logger.LogError(this, LayerNames.Services, "GetFaltAlternativForFalt", string.Format("Error getting fältalternativ for fält id: {0}", faltId), ex);
                throw new ApplicationException(ex.Detail.ServiceError);
            }
            return fa;
        }
    }
}
